-------------------模块区-------------------
local dx_map = require("dx_map")
local dx_thread = require("dx_thread")
local dx_queue = require("dx_queue")
local log = require("mylog")
local gpio_service = require("service.gpio_service")
local network_service = require("service.network_service")
local sqllite3_init = require("sqllite3.sqllite3_init")
config = require("config")
local dx_json = require("dx_json")
local dx_watchdog = require("dx_watchdog")
local time_util = require("time")
local start_service = require("start_service")
require("update_config")

-------------------变量区-------------------
local application = {}

-------------------函数区-------------------
function application.run()
    -- 初始化mqtt队列
    _G.mqtt_queue = dx_queue.create(100)

    log.info("服务开始初始化")
    -- 初始化全局缓存
    _G._G_map = dx_map.create()
    log.info("[全局map]初始化完成")
    -- 初始化读取配置信息！
    config.config_init()
    log.info("[系统配置]初始化完成")

    -- 配置放入全局map
    local g_config_str = dx_json.encode(g_config)
    dx_map.set(_G_map, "g_config_str", g_config_str)
    local vbar_config_t_str = dx_json.encode(vbar_config_t)
    dx_map.set(_G_map, "vbar_config_t_str", vbar_config_t_str)
    log.info("[配置放入全局map]初始化完成")

    -- 初始化mqtt队列
    dx_map.set(_G_map, "mqtt_queue", mqtt_queue)
    log.info("[mqtt队列]初始化完成")

    -- 初始化sqllite
    sqllite3_init.init()
    dx_map.set(_G_map, "sqlite_client", sqlite_client)
    log.info("[sqlite实例]初始化完成")

    -- 初始化网络环境
    network_service.config_net()

    -- 校准时间
    -- time_util.calibration_time()

    dx_map.set(_G_map, "channel_read_queue", channel_read_queue)
    dx_map.set(_G_map, "channel_write_queue", channel_write_queue)

    -- 初始化串口监听
    dx_thread.create("/dx_cc101_app/src/common/serialport_common.lua", {_G_map = _G_map})
    log.info("[串口监听]初始化完成")

    -- 初始化mqtt监听
    dx_thread.create("/dx_cc101_app/src/common/mqtt_common.lua", {_G_map = _G_map})

    -- 初始化&定义gpio
    gpio_service.config_gpio()
    log.info("[gpio]初始化完成")

    -- 设备状态回调
    dx_thread.create("/dx_cc101_app/src/service/keyboard_service.lua", {_G_map = _G_map})
    log.info("[keyboard]初始化完成")

    -- -- 初始化看门狗
    local wdt = dx_watchdog.watchdog_open(1, 0)
    dx_watchdog.watchdog_enable(wdt, 1, true)
    log.info("[看门狗]初始化完成")
    -- 设置看门狗最大喂狗时长
    dx_watchdog.watchdog_start(wdt, 10000)

    -- 判定设备启动状态上送
    start_service.running_status(dx_watchdog.watchdog_is_poweron(wdt))

    while true do
        os.execute("sleep " .. 1)
        if
            (dx_map.get(_G_map, "mqtt_dog") == "Y" or dx_map.get(_G_map, "mqtt_connect_ing") == "Y") and
                dx_map.get(_G_map, "serialport_dog") == "Y"
         then
            -- 子线程通过map中约定的键值喂狗
            dx_watchdog.watchdog_restart(wdt, 1)
            dx_map.set(_G_map, "mqtt_dog", "N")
            dx_map.set(_G_map, "serialport_dog", "N")
        end
    end
end

-- 释放资源
function application.exit()
    gpio_service.exit_gpio()
    sqllite3_init.exit()
end

-------------------主流程-------------------
-- 开始运行主逻辑
application:run()

-- 服务结束前释放资源
application:exit()
